package org.itboys.core.controller.front.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.service.mysql.front.SundayFrontPayService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.pay.weixin.JsPaySignTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/10/23
 * 描述：支付
 */
@Controller
@RequestMapping("/sunday/small/pay")
@Api(value = "pay", description = "支付")
public class SundaySmallPayController extends BaseController {
    @Autowired
    private SundayFrontPayService payService;

    /**
     * 预支付
     *
     * @param request
     * @param orderIds
     * @return
     */
    @RequestMapping(value = "/prepay", method = {RequestMethod.POST})
    @ApiOperation(value = "预支付")
    public void prepay(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "orderIds", required = true) List<Long> orderIds
    ) throws IOException {
        Map<String, Object> orderPayResult = payService.prepay(orderIds);
        ResponseMessage.success(orderPayResult);
    }

    /**
     * 微信-网页-统一下单
     *
     * @param request
     * @param response
     * @param outTradeNo
     */
    @RequestMapping(value = "/wechat/paySign", method = {RequestMethod.POST})
    @ApiOperation(value = "微信统一下单")
    public void wechat_paySign(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "outTradeNo", required = true) String outTradeNo,
            @RequestParam(value = "memberId", required = true) Long memberId
    ) throws Exception {
        JsPaySignTool tool = payService.wechat_paySign(request, outTradeNo, memberId);
        ResponseMessage.success(tool);
    }

    /**
     * 微信回掉
     * @param request
     * @param response
     */
    @RequestMapping(value="/callback/wechat", method = {RequestMethod.POST,RequestMethod.GET})
    public  void callback_wchat(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        try {
            String reuslt =payService.callback_wechat(request);
            AjaxUtils.renderJson(response, reuslt);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("/sunday/weixin/pay/callback/wechat------"+e.getMessage());
            AjaxUtils.renderJson(response, "fail");
        }
    }
}
